package com.cy.store.Controller;


import com.cy.store.Entity.Address;
import com.cy.store.Service.Ex.*;
import com.cy.store.Service.IAddressService;
import com.cy.store.util.JsonResult;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller
@RestController
@RequestMapping("/address")
public class AddressController extends BaseController{
    @Autowired
    private IAddressService addressService;



    @RequestMapping("/add_new_address")
    public JsonResult<Address> addAddress(HttpSession session,Address address) throws AddressCountLimitException, InsertException {
        Integer uid = getuidFromSession(session);
        String username=getusernameFromSession(session);
        addressService.addNewAddress(uid,username,address);
        return new JsonResult<>(OK,address);
    }


    @RequestMapping({"","/"})
    public JsonResult<List<Address>> getAddressList(HttpSession session) throws AddressCountLimitException, InsertException {
        Integer uid = getuidFromSession(session);
        List<Address> list =addressService.getByUid(uid);
        return new JsonResult<>(OK,list);
    }

    //RestFul风格的请求编写
    @RequestMapping("/{aid}/set_default")
    public JsonResult<Void> setDefault(
            @PathVariable("aid") Integer aid, HttpSession session) throws AccessDeniedException, AddressNotFoundException, UpdateException {
        addressService.setDefault(
                aid,
                getuidFromSession(session),
                getusernameFromSession(session));
        return new JsonResult<>(OK);
    }


    @RequestMapping("/{aid}/delete")
    public JsonResult<Void> delete(@PathVariable("aid") Integer aid,HttpSession session) throws AccessDeniedException, DeleteException, AddressNotFoundException, UpdateException {
        addressService.delete(
                aid,
                getuidFromSession(session),
                getusernameFromSession(session));
        return new JsonResult<>(OK);
    }


}
